﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Reflection;
namespace LotteryAdmin
{
    public partial class ChangeNews : ChildWindow
    {
        LotteryAdminClient srv;
        bool isFirstLoad = true;
        public ChangeNews()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            srv.UpdateNewsCompleted += new System.EventHandler<UpdateNewsCompletedEventArgs>(srv_UpdateNewsCompleted);
            this.dataForm1.Loaded += new RoutedEventHandler(dataForm1_Loaded);
        }

        void dataForm1_Loaded(object sender, RoutedEventArgs e)
        {
            if (news.NewsType == 0)
                (dataForm1.FindNameInContent("cbxType") as ComboBox).SelectedIndex = 0;
            if (news.NewsType == 1)
                (dataForm1.FindNameInContent("cbxType") as ComboBox).SelectedIndex = 1;
            if (news.NewsType == 2)
                (dataForm1.FindNameInContent("cbxType") as ComboBox).SelectedIndex = 2;
        }

        void srv_UpdateNewsCompleted(object sender, UpdateNewsCompletedEventArgs e)
        {
            if (e.Result)
            {
                orgNews = ModelCopy.CopyValueForModel<News>(news, orgNews);
                this.DialogResult = true;
            }
            else
                MessageBox.Show("操作失败,网络异常或者服务器不可连接!");
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {

            if (dataForm1.ValidateItem())
            {
                if ((dataForm1.FindNameInContent("cbxType") as ComboBox).SelectedIndex != 2)
                {
                    news.UrlAddress = string.Empty;
                }
                srv.UpdateNewsAsync(news);
            }
        }
        public void ShowAddWindow()
        {
            this.Title = "平台公告新增";
            news = new  News();
            this.dataForm1.CurrentItem = news;
            base.Show();
        }
        News news;
        News orgNews;
        public void ShowUpdateWindow(News info)
        {
            this.Title = "平台公告修改";
            orgNews = info;
            news = ModelCopy.CopyValueForModel<News>(info);
            this.dataForm1.CurrentItem = news;
            base.Show();
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void cbxType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (isFirstLoad)
            {
                if (news.NewsType == 2)
                    (dataForm1.FindNameInContent("df_url") as DataField).Visibility = Visibility.Visible;
                else
                    (dataForm1.FindNameInContent("df_url") as DataField).Visibility = Visibility.Collapsed;
                isFirstLoad = false;
            }
            else
            {
                if (((sender as ComboBox).SelectedItem as ComboBoxItem).Content.ToString()=="链接")
                    (dataForm1.FindNameInContent("df_url") as DataField).Visibility = Visibility.Visible;
                else
                    (dataForm1.FindNameInContent("df_url") as DataField).Visibility = Visibility.Collapsed;
                if ((dataForm1.FindNameInContent("cbxType") as ComboBox).SelectedIndex == 0)
                {
                    news.NewsType = 0;
                }
                if ((dataForm1.FindNameInContent("cbxType") as ComboBox).SelectedIndex == 1)
                {
                    news.NewsType = 1;
                }
                if ((dataForm1.FindNameInContent("cbxType") as ComboBox).SelectedIndex == 2)
                {
                    news.NewsType = 2;
                }
            }
        }
    }
}

